package com.fac.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fac.constant.CommonConstant;
import com.fac.constant.ExceptionConstant;
import com.fac.dto.CategoryCreateDTO;
import com.fac.dto.CategoryPageQueryDTO;
import com.fac.entity.EquipmentCategory;
import com.fac.exception.BusinessException;
import com.fac.mapper.EquipmentCategoryMapper;
import com.fac.result.PageResult;
import com.fac.service.EquipmentCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 设备分类服务实现类
 */
@Service
@Slf4j
public class EquipmentCategoryServiceImpl implements EquipmentCategoryService {

    @Autowired
    private EquipmentCategoryMapper equipmentCategoryMapper;

    /**
     * 创建设备分类
     *
     * @param categoryCreateDTO 设备分类创建DTO
     * @return 创建的设备分类
     */
    @Override
    public EquipmentCategory create(CategoryCreateDTO categoryCreateDTO) {
        log.info("创建设备分类：{}", categoryCreateDTO);

        // 检查编码是否已存在
        LambdaQueryWrapper<EquipmentCategory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EquipmentCategory::getCode, categoryCreateDTO.getCode());
        if (equipmentCategoryMapper.selectCount(queryWrapper) > 0) {
            throw new BusinessException(ExceptionConstant.CATEGORY_CODE_EXISTS);
        }

        // 如果有父分类，检查父分类是否存在
        if (categoryCreateDTO.getParentId() != null) {
            EquipmentCategory parentCategory = equipmentCategoryMapper.selectById(categoryCreateDTO.getParentId());
            if (parentCategory == null) {
                throw new BusinessException(ExceptionConstant.PARENT_CATEGORY_NOT_FOUND);
            }
        }

        // 创建分类实体
        EquipmentCategory category = new EquipmentCategory();
        BeanUtils.copyProperties(categoryCreateDTO, category);

        // 设置状态为启用
        category.setStatus(CommonConstant.STATUS_ENABLE);

        // 保存分类
        equipmentCategoryMapper.insert(category);

        return category;
    }

    /**
     * 分页查询设备分类
     *
     * @param dto 分页查询条件
     * @return 分页结果
     */
    @Override
    public PageResult pageQuery(CategoryPageQueryDTO dto) {
        log.info("分页查询设备分类：{}", dto);

        // 构建查询条件
        LambdaQueryWrapper<EquipmentCategory> queryWrapper = new LambdaQueryWrapper<>();
        
        // 添加父分类查询条件
        if (dto.getParentId() != null) {
            queryWrapper.eq(EquipmentCategory::getParentId, dto.getParentId());
        }

        // 添加关键字查询条件
        String keyword = dto.getKeyword();
        if (StringUtils.isNotEmpty(keyword)) {
            queryWrapper.like(EquipmentCategory::getName, keyword)
                    .or()
                    .like(EquipmentCategory::getCode, keyword);
        }

        // 添加状态查询条件
        if (dto.getStatus() != null) {
            queryWrapper.eq(EquipmentCategory::getStatus, dto.getStatus());
        }

        // 添加排序条件
        queryWrapper.orderByAsc(EquipmentCategory::getSort);

        // 创建分页对��
        Page<EquipmentCategory> page = new Page<>(dto.getPage(), dto.getSize());

        // 执行分页查询
        Page<EquipmentCategory> categoryPage = equipmentCategoryMapper.selectPage(page, queryWrapper);

        return new PageResult(categoryPage.getTotal(), categoryPage.getRecords());
    }

    /**
     * 根据ID查询设备分类
     *
     * @param categoryId 分类ID
     * @return 设备分类
     */
    @Override
    public EquipmentCategory getById(Long categoryId) {
        return equipmentCategoryMapper.selectById(categoryId);
    }

    /**
     * 删除设备分类
     *
     * @param categoryId 分类ID
     */
    @Override
    public void deleteById(Long categoryId) {
        // 检查是否有子分类
        LambdaQueryWrapper<EquipmentCategory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EquipmentCategory::getParentId, categoryId);
        if (equipmentCategoryMapper.selectCount(queryWrapper) > 0) {
            throw new BusinessException(ExceptionConstant.CATEGORY_HAS_CHILDREN);
        }

        // 删除分类
        equipmentCategoryMapper.deleteById(categoryId);
    }
} 